import network
import socket
from machine import Pin
import ure
from neopixel import NeoPixel
import random
import time

def npx(rgb):    
    print('------npx-----')
    print(rgb)
    r,g,b=rgb
    for i in range(0,30):
        np[i]=(int(r),int(g),int(b))
        np.write()
    '''
    cl1=int(R)
    cl2=int(G)
    cl3=int(B)
    for i in range(0,120):
        np[i]=(cl1, cl2, cl3)
        np.write()
        time.sleep_ms(10)'''

ap=network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid='ESP1',authmode=3,password='12345678')
 
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(('192.168.4.1', 80))
s.listen(5)

htmls = open("s.html")
html= htmls.read()      
htmls.close()

pin = Pin(4, Pin.OUT)   # set GPIO0 to output to drive NeoPixels
np = NeoPixel(pin, 120)   # create NeoPixel driver on GPIO0 for 8 pixels

np.fill((255,0,0))
np.write()
time.sleep_ms(500)
np.fill((0,0,0))
np.write()

while 1:
    print('i am waiting...')
    c,a=s.accept()#返回两个值 #闭塞只有有数据才会执行
    request=c.recv(1024)
    request = str(request)
    print(request)
    res=ure.search('cl=rgb(.*?) HTTP',request)
    if res:
        res=res.group(1)
        res=res.replace('(','')
        res=res.replace(')','')
        print(type(res),'============',res)
        list_res=res.split(',')
        npx(list_res)
    try:
        c.send(html)
    except:
        print('no')
    c.close()
s.close()    

